package com.aust.furniture.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aust.furniture.entity.Furniture;
import com.aust.furniture.entity.Returngoods;
import com.aust.furniture.mapper.FurnitureMapper;
import com.aust.furniture.mapper.ReturngoodsMapper;
import com.aust.furniture.service.IReturngoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 收到退货service实现类
 */
@Service
public class ReturngoodsServiceImpl extends ServiceImpl<ReturngoodsMapper, Returngoods> implements IReturngoodsService {
    @Autowired
    private ReturngoodsMapper returngoodsMapper;
    @Autowired
    private FurnitureMapper furnitureMapper;
    /**
     * 分页查询收到退货数据
     *
     * @param pageNum  第几页
     * @param pageSize 每页多少条数据
     * @param param    查询参数-收到退货名称
     * @return
     */
    @Override
    public IPage<Returngoods> selectReturngoodsPage(int pageNum, int pageSize, String param) {
        QueryWrapper<Returngoods> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(param)){
            queryWrapper.like("furniture_name",param);
        }
        Page<Returngoods> page = new Page<>(pageNum,pageSize);
        return returngoodsMapper.selectPage(page,queryWrapper);
    }

    /**
     * 新增一条收到退货信息
     *
     * @param returngoods
     */
    @Transactional
    @Override
    public int addReturngoods(Returngoods returngoods) {
        String furnitureName = returngoods.getFurnitureName();//家具名称
        Furniture furniture = furnitureMapper.selectOne(new QueryWrapper<Furniture>().eq("name", furnitureName));
        returngoods.setNumber(furniture.getNumber());
        Float price = furniture.getPrice();
        returngoods.setTotalPrice(returngoods.getCount()*price);
        //修改库存
        furniture.setStock(furniture.getStock()+returngoods.getCount());
        furnitureMapper.updateById(furniture);
        return returngoodsMapper.insert(returngoods);
    }

    /**
     * 修改一条收到退货信息
     *
     * @param returngoods
     */
    @Transactional
    @Override
    public int editReturngoods(Returngoods returngoods) {
        Returngoods oldReturnGood = returngoodsMapper.selectById(returngoods.getId());
        Integer oldCount = oldReturnGood.getCount();
        Integer newCount = returngoods.getCount();
        //修改库存
        Furniture furniture = furnitureMapper.selectOne(new QueryWrapper<Furniture>().eq("name", returngoods.getFurnitureName()));
        furniture.setStock(furniture.getStock()+(newCount-oldCount));
        furnitureMapper.updateById(furniture);
        returngoods.setNumber(furniture.getNumber());
        Float price = furniture.getPrice();
        returngoods.setTotalPrice(returngoods.getCount()*price);
        return returngoodsMapper.updateById(returngoods);
    }

    /**
     * 根据主键id查询一个收到退货对象
     *
     * @param id
     * @return
     */
    @Override
    public Returngoods queryReturngoodsById(Integer id) {
        return returngoodsMapper.selectById(id);
    }

    /**
     * 根据主键id删除一个收到退货对象
     *
     * @param id
     * @return
     */
    @Transactional
    @Override
    public int delReturngoodsById(Integer id) {
        //删除之前先查询该条数据
        Returngoods returngoods = returngoodsMapper.selectById(id);
        //修改库存
        Furniture furniture = furnitureMapper.selectOne(new QueryWrapper<Furniture>().eq("name", returngoods.getFurnitureName()));
        furniture.setStock(furniture.getStock()-returngoods.getCount());
        furnitureMapper.updateById(furniture);
        //删除该条数据
        return returngoodsMapper.deleteById(id);
    }

}
